﻿Imports Q7P1TTS.Lib
Public Class FeedbackList
    Dim currentPage, maxPage, rowPerPage As Integer
    Dim dynamicSource As New List(Of Complaint)
    Dim loadType As Integer

    Public Sub New(ByVal type As Integer)
        InitializeComponent()
        loadType = type
    End Sub
    Private Sub FeedbackList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cboStatusSelector.Items.Add("All")
        cboStatusSelector.Items.Add(ComplaintStatus.Pending)
        cboStatusSelector.Items.Add(ComplaintStatus.UnderInvestigation)
        cboStatusSelector.Items.Add(ComplaintStatus.InvestigationCompleted)
        cboStatusSelector.Items.Add(ComplaintStatus.Closed)
        cboStatusSelector.Items.Add(ComplaintStatus.Cancelled)
        rowPerPage = Convert.ToInt32(My.Settings.ROW_PER_PAGE)
        cboStatusSelector.SelectedIndex = 0

        Dim mainForm As MainForm = Me.FindForm()
        btnEdit.Visible = mainForm.isEmployee Or mainForm.isAdmin
        btnDelete.Visible = mainForm.isCREmployeeLead Or mainForm.isAdmin
        btnDetail.Visible = mainForm.isCustomer
        DataBind()
    End Sub

    Private Sub DataBind()
        Dim items As New List(Of Complaint)
        If loadType = 0 Then
            items = ComplaintController.load().OrderByDescending(Function(c As Complaint) c.SubmitOn).ToList()
        ElseIf loadType = 1 Then
            Dim emp As Employee = SessionHelper.Sessions("USER_INFO")
            items = ComplaintController.loadByEmp(emp.ID).OrderByDescending(Function(c As Complaint) c.SubmitOn).ToList()
        ElseIf loadType = 2 Then
            items = ComplaintController.loadByStatus(ComplaintStatus.Pending).OrderByDescending(Function(c As Complaint) c.SubmitOn).ToList()
            cboStatusSelector.SelectedIndex = cboStatusSelector.Items.IndexOf(ComplaintStatus.Pending)
            cboStatusSelector.Enabled = False
        ElseIf loadType = 3 Then
            cboStatusSelector.Enabled = False
            Try
                Dim cust As Customer = SessionHelper.Sessions("USER_INFO")
                items = ComplaintController.load().Where(Function(c As Complaint) c.SubmitBy = cust.ID).OrderByDescending(Function(c As Complaint) c.SubmitOn).ToList()
            Catch ex As Exception

            End Try
        End If

        If cboStatusSelector.SelectedItem.ToString() <> "All" Then
            items = items.Where(Function(i As Complaint) i.Status = cboStatusSelector.SelectedItem.ToString().Trim()).ToList()
        End If

        If Not String.IsNullOrEmpty(txtSearch.Text) Then
            Dim regEx As New System.Text.RegularExpressions.Regex(txtSearch.Text)
            items = items.Where(Function(i As Complaint) regEx.IsMatch(i.RefNumber)).Distinct().ToList()
        End If
        grdFeedbackList.AutoGenerateColumns = False
        grdFeedbackList.DataSource = items
        dynamicSource = items
        maxPage = Math.Ceiling(dynamicSource.Count / rowPerPage)
        cboPageSelector.Items.Clear()
        For i As Integer = 1 To maxPage Step 1
            cboPageSelector.Items.Add(i)
        Next
        If cboPageSelector.Items.Count > 0 Then
            cboPageSelector.SelectedIndex = 0
        End If
        Paging()
    End Sub

    Private Sub Paging()
        lblMaxPage.Text = "/" & maxPage.ToString()
        grdFeedbackList.DataSource = dynamicSource.Skip((currentPage - 1) * rowPerPage).Take(rowPerPage).ToList()

        btnFirst.Enabled = (currentPage > 1)
        btnPrev.Enabled = (currentPage > 1)
        btnNext.Enabled = (currentPage < maxPage)
        btnLast.Enabled = (currentPage < maxPage)
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        DataBind()
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        Dim main As MainForm = Me.FindForm()
        If grdFeedbackList.SelectedRows.Count <> 1 Then
            MessageBox.Show("Please select 1 record.")
        Else
            Dim obj As Complaint = grdFeedbackList.SelectedRows(0).DataBoundItem
            main.GoToForm(New FeedBackForm(obj.ID), "Update Complaint: " & obj.RefNumber)
        End If
    End Sub

    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        cboPageSelector.SelectedIndex = 0
    End Sub

    Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
        cboPageSelector.SelectedIndex = cboPageSelector.SelectedIndex - 1
    End Sub

    Private Sub cboPageSelector_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboPageSelector.SelectedIndexChanged
        currentPage = Convert.ToInt32(cboPageSelector.SelectedItem)
        Paging()
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        cboPageSelector.SelectedIndex = cboPageSelector.SelectedIndex + 1
    End Sub

    Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
        cboPageSelector.SelectedIndex = cboPageSelector.Items.Count - 1
    End Sub

    Private Sub grdFeedbackList_RowPrePaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles grdFeedbackList.RowPrePaint
        Try
            Dim com As Complaint = grdFeedbackList.Rows(e.RowIndex).DataBoundItem
            grdFeedbackList.Rows(e.RowIndex).Cells("Category").Value = com.Category.Name
            grdFeedbackList.Rows(e.RowIndex).Cells("Source").Value = com.Source.Name
            grdFeedbackList.Rows(e.RowIndex).Cells("SubmitOn").Value = ConvertHelper.ToDateTime(com.SubmitOn, "MM/dd/yyyy")
            grdFeedbackList.Rows(e.RowIndex).Cells("SubmitBy").Value = com.Customer.FirstName & " " & com.Customer.LastName
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If grdFeedbackList.SelectedRows.Count <> 1 Then
            MessageBox.Show("Please select 1 record.")
        Else
            If MessageBox.Show("Are you sure", "Confirmation", MessageBoxButtons.YesNo) = DialogResult.Yes Then
                Dim obj As Complaint = grdFeedbackList.SelectedRows(0).DataBoundItem
                If ComplaintController.delete(obj) > 0 Then
                    DataBind()
                Else
                    MessageBox.Show("There is an unknown error when deleting data. Please contact administrator.")
                End If

            End If
        End If
    End Sub

    Private Sub btnDetail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDetail.Click
        If grdFeedbackList.SelectedRows.Count <> 1 Then
            MessageBox.Show("Please select 1 record.")
        Else
            Dim obj As Complaint = grdFeedbackList.SelectedRows(0).DataBoundItem
            Dim popup As New PopupHelper(New FeedbackDetail(obj.ID), "Feedback Detail")
            popup.ShowDialog(Me.FindForm())
        End If
    End Sub
End Class
